from rest_framework import serializers
from .models import (
    NotificationTemplate, Notification,
    EmailLog, SystemAnnouncement, UserNotificationPreference
)


class NotificationTemplateSerializer(serializers.ModelSerializer):
    class Meta:
        model = NotificationTemplate
        fields = [
            'id', 'name', 'template_type', 'channel', 'subject',
            'content', 'variables', 'is_active', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class NotificationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notification
        fields = [
            'id', 'title', 'message', 'notification_type', 'priority',
            'is_read', 'action_url', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class NotificationCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notification
        fields = [
            'user', 'title', 'message', 'notification_type',
            'priority', 'action_url', 'expires_at'
        ]


class EmailLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = EmailLog
        fields = [
            'id', 'recipient', 'subject', 'status', 'template_name',
            'sent_at', 'error_message'
        ]
        read_only_fields = ['id', 'sent_at']


class SystemAnnouncementSerializer(serializers.ModelSerializer):
    class Meta:
        model = SystemAnnouncement
        fields = [
            'id', 'title', 'content', 'announcement_type', 'priority',
            'start_date', 'end_date', 'action_url', 'is_active', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class SystemAnnouncementCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = SystemAnnouncement
        fields = [
            'title', 'content', 'announcement_type', 'priority',
            'start_date', 'end_date', 'action_url'
        ]


class UserNotificationPreferenceSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserNotificationPreference
        fields = [
            'email_notifications', 'push_notifications', 'sms_notifications',
            'order_notifications', 'promotion_notifications', 'system_notifications'
        ]


class NotificationStatsSerializer(serializers.Serializer):
    total_notifications = serializers.IntegerField()
    unread_notifications = serializers.IntegerField()
    read_notifications = serializers.IntegerField()
    notifications_by_type = serializers.DictField()


class EmailStatsSerializer(serializers.Serializer):
    total_sent = serializers.IntegerField()
    total_failed = serializers.IntegerField()
    success_rate = serializers.FloatField()
    templates_usage = serializers.DictField()


class AnnouncementListSerializer(serializers.ModelSerializer):
    class Meta:
        model = SystemAnnouncement
        fields = [
            'id', 'title', 'content', 'announcement_type', 'priority',
            'start_date', 'end_date', 'action_url', 'created_at'
        ]


class BulkNotificationSerializer(serializers.Serializer):
    users = serializers.ListField(
        child=serializers.IntegerField(),
        help_text="用户ID列表"
    )
    title = serializers.CharField(max_length=200)
    message = serializers.CharField()
    notification_type = serializers.ChoiceField(
        choices=['info', 'warning', 'error', 'success']
    )
    priority = serializers.ChoiceField(
        choices=['low', 'medium', 'high', 'urgent']
    )
    action_url = serializers.URLField(required=False, allow_blank=True)
    expires_at = serializers.DateTimeField(required=False)


class NotificationFilterSerializer(serializers.Serializer):
    notification_type = serializers.ChoiceField(
        choices=['info', 'warning', 'error', 'success'],
        required=False
    )
    priority = serializers.ChoiceField(
        choices=['low', 'medium', 'high', 'urgent'],
        required=False
    )
    is_read = serializers.BooleanField(required=False)
    start_date = serializers.DateField(required=False)
    end_date = serializers.DateField(required=False)


class EmailTemplateTestSerializer(serializers.Serializer):
    template_name = serializers.CharField()
    recipient = serializers.EmailField()
    variables = serializers.DictField(required=False, default={})
